using System;
using System.Windows;
using System.Windows.Navigation;
using System.Windows.Threading;
using NetworkVisualizer.Controls;
using StreamToMe.Flow.Windows.Utils;

namespace StreamToMe.Flow.Windows
{
    /// <summary>
    /// Interaction logic for WebNode.xaml
    /// </summary>
    public partial class WebNode
    {
        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof (string), typeof (WebNode), new UIPropertyMetadata(""));

        public static readonly DependencyProperty UrlProperty =
            DependencyProperty.Register(
                "Url",
                typeof (String),
                typeof (WebNode),
                new UIPropertyMetadata(
                    null,
                    (sender, args) => ((WebNode) sender)._Browser.Source =
                                      new Uri(args.NewValue as string)));

        private bool _Initializing;

        public WebNode()
        {
            InitializeComponent();
        }

        public String Url
        {
            get { return (String) GetValue(UrlProperty); }
            set { SetValue(UrlProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Url.  This enables animation, styling, binding, etc...

        public string Label
        {
            get { return (string) GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }


        public override void BeginInit()
        {
            _Initializing = true;
            base.BeginInit();
        }

        private void _Browser_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (_Initializing) return;
            var uri = e.Uri;
            var node = new WebNode();
            node.BeginInit();
            node.Url = uri.AbsoluteUri;
            var panel = (NetworkPanel)Parent;
            panel.Children.Add(node);
            panel.Links.Add(this, node);
            panel.CenterNode = node;
            node.EndInit();
            e.Cancel = true;
            LoadThumbnail();
            _Thumbnail.Visibility = Visibility.Visible;
            _Browser.Visibility = Visibility.Collapsed;
            node._Browser.Visibility = Visibility.Visible;
        }

        private void _Browser_Loaded(object sender, NavigationEventArgs e)
        {
            LoadThumbnail();
            _Initializing = false;
        }

        private void LoadThumbnail()
        {
            var visibility = _Browser.Visibility;
            var frame = new DispatcherFrame {Continue = false};
            frame.Dispatcher.Invoke(() => _Browser.Visibility = Visibility.Visible);
            Dispatcher.PushFrame(frame);
            _Thumbnail.Source = _Browser.MakeBitmapSource();
            _Browser.Visibility = visibility;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _Browser.Visibility = Visibility.Visible;
            var panel = (NetworkPanel)Parent;
            panel.CenterNode = this;
            _Thumbnail.Visibility = Visibility.Collapsed;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            LoadThumbnail();
            _Browser.Visibility = Visibility.Collapsed;
            _Thumbnail.Visibility = Visibility.Visible;
        }
    }
}